package com.itxx.keycase.blockingqueue;

import java.util.concurrent.LinkedBlockingQueue;

//消费者
class Foodie1 extends Thread {
    private LinkedBlockingQueue<String> link;

    public Foodie1() {
    }

    public Foodie1(LinkedBlockingQueue<String> link) {
        this.link = link;
    }

    @Override
    public void run() {
        String s = null;
        for (int i = 0; i <= 5; i++) {
            try {
                s = link.take();
                System.out.println(getName() + "吃掉第"+i+"个" + s);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

    }
}


//生产者
class Cooker1 extends Thread {
    private LinkedBlockingQueue<String> link;

    public Cooker1() {
    }

    public Cooker1(LinkedBlockingQueue<String> link) {
        this.link = link;
    }

    @Override
    public void run() {
        for (int i = 0; i <= 5; i++) {

            try {
                link.put("汉堡包");
                System.out.println(getName() + "新上第"+i+"个汉堡包");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}


public class Test2 {
    public static void main(String[] args) {
        LinkedBlockingQueue<String> link = new LinkedBlockingQueue<>(1);

        Cooker1 c = new Cooker1(link);
        Foodie1 f = new Foodie1(link);
        c.setName("生产者:");
        f.setName("消费者:");
        c.start();
        f.start();
    }
}
